package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartConterllor {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /*
     * 查询购物车*/
    @GetMapping("/list")
    public R<List<ShoppingCart>> seleteList() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, ShoppingCart::getUserId, userId);
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);

    }

    /*
     * 添加购物车*/
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //获取菜品id
        Long dishId = shoppingCart.getDishId();

        //查询用户购物车
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);

        //判断添加的菜品还是套餐
        if (dishId != null) {
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断用户购物车中添加套餐或者菜品的数量
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);
        if (cartServiceOne != null) {
            //购物车中相同的菜品或者套餐大于1
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            //更新购物车
            shoppingCartService.updateById(cartServiceOne);
        } else {
            //没有相同的菜品或者套餐
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /*
     * 取消购物车*/
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();

        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //查询用户购物车
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, currentId);
        //判断添加的是菜品还是套餐
        if (dishId != null) {
            //添加的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //判断用户购物车中有相同的菜品和套餐
        ShoppingCart cartServiceOne = shoppingCartService.getOne(wrapper);
        Integer number = cartServiceOne.getNumber();
        if (number == 1) {
            //执行删除操作
            shoppingCartService.remove(wrapper);
        } else {

            shoppingCartService.updateById(cartServiceOne);
        }
        return R.success(cartServiceOne);

    }

    /*
     * 清空购物车*/
    @DeleteMapping("/clean")
    public R<String> delete() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }

}
